const fs = require('fs')
const path = require('path')
const express = require('express')
const app = express()
const bodyParser = require("body-parser")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/ceshi', function (req, res) {
    res.send('hello woradld')
})
//登录响应
app.post('/login', function (req, res) {
    const { username, password } = req.body
    let userList = fs.readFileSync(path.join(__dirname, "./data/user.json")).toString()
    userList = userList ? JSON.parse(userList) : []
    let result = userList.find(item => item.username == username)
    if (result) {
        if (result.password == password) {
            res.send(
                {
                    code: 0,
                    msg: "登录成功"
                }
            )
        } else {
            res.send(
                {
                    code: 1,
                    msg: "密码错误"
                }
            )
        }
    } else {
        res.send(
            {
                code: 2,
                msg: "用户名不存在"
            }
        )
    }
})
//注册接口
app.post('/register', function (req, res) {
    const { username, password } = req.body
    let userList = fs.readFileSync(path.join(__dirname, "./data/user.json")).toString()
    userList = userList ? JSON.parse(userList) : []
    let result = userList.some(item => item.username == username)
    if (result) {
        res.send(
            {
                code: 0,
                msg: "用户名已存在"
            }
        )
    } else {
        userList.push({
            username,
            password
        })
        fs.writeFileSync(path.join(__dirname, "./data/user.json"), JSON.stringify(userList))
        res.send(
            {
                code: 1,
                msg: "注册成功"
            }
        )
    }
})
//发布博客
app.put("/add", (req, res) => {
    let list = fs.readFileSync(path.join(__dirname, "./data/list.json")).toString()
    list = list ? JSON.parse(list) : []
    const { username, text } = req.body
    let i = list.findIndex(it => it.username == username)
    //用户发布多条博客
    if(i!==-1){
        if (list[i].username == username) {
            list[i].text.push({
                id:list[i].text.length+1,
                text
            })
            fs.writeFileSync(path.join(__dirname, './data/list.json'), JSON.stringify(list, null, '\t'))
            res.send(
                {
                    code: 1,
                    msg: "博客成功发布"
                }
            )
        }
    }else{
    //不同用户发布博客
        list.push(
            {
                id:list.length +1,
                username,
                text:[
                    {
                        id:1,
                        text
                    }
                ]
            }
        )
        fs.writeFileSync(path.join(__dirname, './data/list.json'), JSON.stringify(list, null, '\t'))
        res.send(
            {
                code: 1,
                msg: "博客成功发布"
            }
        )
    }
})
//获取博客列表
app.get("/getBlogsList",(req,res)=>{
    let list = fs.readFileSync(path.join(__dirname, "./data/list.json")).toString()
    list = JSON.parse(list)
    res.send({
        code:'0',
        msg:"收到请求,成功发送博客列表",
        data:list
    })
})
//点赞接口
app.post("/like",(req,res)=>{
    let list = fs.readFileSync(path.join(__dirname, "./data/likeList.json")).toString()
    list = JSON.parse(list)
    const {username,author} = req.body
    let i = 0;
    for(i;i<list.length;i++){
        if(list[i].username === username){
            break
        }
    }
    if(i!==list.length){
        let n = list[i].likeList.findIndex(ite => ite.author == author);
        if(n === -1){
            list[i].likeList.push({
                id:list[i].likeList.length + 1,
                author
            })
            fs.writeFileSync(path.join(__dirname,"./data/likeList.json"), JSON.stringify(list,null,'\t'))
            res.send({
                code:1,
                msg:'点赞成功'
            })
        }else{
            list[i].likeList.splice(n,1)
            fs.writeFileSync(path.join(__dirname,"./data/likeList.json"), JSON.stringify(list,null,'\t'))
            res.send({
                code:2,
                msg:'取消点赞'
            })
        }
    }
})
app.listen(8080, () => { console.log(`run as http://localhost:8080`) })

